package com.xiaoyu.lanling.feature.register.activity

import `in`.srain.cube.util.UTUtil
import android.os.Bundle
import androidx.viewpager.widget.ViewPager
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.register.RegisterFirstNextButtonClickEvent
import com.xiaoyu.lanling.event.register.RegisterSecondBackButtonClickEvent
import com.xiaoyu.lanling.feature.register.fragment.RegisterFirstFragment
import com.xiaoyu.lanling.feature.register.fragment.RegisterSecondFragment
import com.xiaoyu.lanling.view.FragmentAdapter
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.activity_register.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class RegisterActivity : AppCompatToolbarActivity() {

    private var mAdapter: FragmentAdapter? = null
    private var fragments: MutableList<OnShowFragment> = mutableListOf<OnShowFragment>().apply {
        add(RegisterFirstFragment.create(supportFragmentManager))
        add(RegisterSecondFragment.create(supportFragmentManager))
    }

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.activity_register)

        initView()
        initEvent()
        logEventForEnter()
    }

    private fun logEventForEnter() {
        val loginType: String? = intent.getStringExtra(Constant.IntentKey.KEY_LOGIN_TYPE)

        val params = Bundle()
        params.putString("login_type", loginType)
        UTUtil.getInstance().logEvent("enter_register_page", params)
    }

    private fun initView() {
        val titles = mutableListOf<CharSequence>()
        titles.add("first")
        titles.add("second")

        mAdapter = FragmentAdapter(supportFragmentManager, fragments, titles)
        view_pager.adapter = mAdapter
        view_pager.offscreenPageLimit = fragments.size
        view_pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                mAdapter?.tryShowOnIndex(position)
            }
        })
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: RegisterFirstNextButtonClickEvent) {
                if (fragments[view_pager.currentItem] is RegisterFirstFragment) {
                    view_pager.currentItem += 1
                }
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: RegisterSecondBackButtonClickEvent) {
                if (fragments[view_pager.currentItem] is RegisterSecondFragment) {
                    view_pager.currentItem -= 1
                }
            }
        })
    }
}